package android.medicalcloud.com.yiapp.base.net;

import android.medicalcloud.com.yiapp.base.exc.BaseException;
import android.medicalcloud.com.yiapp.base.net.lisener.HttpNetCallBack;
import android.medicalcloud.com.yiapp.base.net.lisener.NetCallback;
import android.medicalcloud.com.yiapp.base.utils.Logger;
import android.medicalcloud.com.yiapp.base.utils.MapUtils;
import android.medicalcloud.com.yiapp.base.utils.StringTools;

import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by wanggenlin on 17-6-2.
 */
public class OkHttpRequest {

    protected static String get(String urlStr,Map <String,String> header) {
        OkHttpClient mOkHttpClient=new OkHttpClient();
        Request.Builder requestBuilder = new Request.Builder().url(urlStr);
        //可以省略，默认是GET请求
        requestBuilder.method("GET",null);
        setHeader(requestBuilder,header);
        Request request = requestBuilder.build();

        Call mcall= mOkHttpClient.newCall(request);
        try {
            Response response = mcall.execute();
            return   response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
            Logger.e(e.getLocalizedMessage(),e);
        }
        return "";
    }





    static  void setHeader(Request.Builder conn,Map <String,String> header){
        if (header==null){
            return;
        }

        Iterator iter = header.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = entry.getKey().toString();
            String val = entry.getValue().toString();
            Logger.e("header=="+key+" "+val);

            conn.addHeader(key, val);
        }
        conn.addHeader("Content-Type", "application/json;charset=UTF-8");
    }

    public static String post(String actionUrl, Map<String, String> headParams,
                              Map<String, Object> params){

        OkHttpClient mOkHttpClient=new OkHttpClient();
        FormBody.Builder builder =  new FormBody.Builder();
        setPostParam(builder,params);
        RequestBody requestBuilder = builder.build();

        //可以省略，默认是GET请求

//        setHeader(requestBuilder,headParams);

        Request.Builder requestBuild = new Request.Builder();
        setHeader(requestBuild,headParams);
        Request request = requestBuild
                .url(actionUrl)
                .post(requestBuilder)
                .build();
        Call mcall= mOkHttpClient.newCall(request);
        try {
            Response response = mcall.execute();
            return   response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    static  void setPostParam(FormBody.Builder conn,Map <String,Object> param){
        if (param==null){
            return;
        }

        Iterator iter = param.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = entry.getKey().toString();
            String val = entry.getValue().toString();
            Logger.e("header=="+key+" "+val);

            conn.add(key, val);
        }
    }




}
